package com.emergency.db.service.impl.item;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.item.ItemDetailDao;
import com.emergency.db.domain.item.ItemDetail;
import com.emergency.db.service.item.ItemDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ItemDetailServiceImpl implements ItemDetailService {
    @Autowired
    private ItemDetailDao itemDetailDao;

    @Autowired
    private IdWorker idWorker;

    //保存和更新
    public Long save(ItemDetail itemDetail) {
        if (CommonUtils.isNullOrEmpty(itemDetail.getId())) {
            itemDetail.setId(idWorker.nextId());
        }
        //保存装备细则信息
        itemDetailDao.save(itemDetail);
        return itemDetail.getId();
    }


    //查询单项
    public ItemDetail findByItem(Long id) {
        return itemDetailDao.findByItem(id);
    }

    //删除
    public void delete(Long id) {
        itemDetailDao.deleteById(id);
    }

    //分页查询
    @Override
    public Page<ItemDetail> findList(Map searchMap, int page, int size) {
        Specification<ItemDetail> specification = createSpecification(searchMap);
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        return itemDetailDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<ItemDetail> createSpecification(Map searchMap) {
        return new Specification<ItemDetail>() {

            @Override
            public Predicate toPredicate(Root<ItemDetail> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                // 保存状态
                if (searchMap.get("saveStatus") != null && !"".equals(searchMap.get("saveStatus"))) {
                    predicateList.add(criteriaBuilder.equal(root.get("saveStatus").as(String.class), (String) searchMap.get("saveStatus")));
                }
                // 设备状态
                if (searchMap.get("itemStatus") != null && !"".equals(searchMap.get("itemStatus"))) {
                    predicateList.add(criteriaBuilder.equal(root.get("itemStatus").as(String.class), (String) searchMap.get("itemStatus")));
                }
                //设备用途
                if (searchMap.get("itemUse") != null && !"".equals(searchMap.get("itemUse"))) {
                    predicateList.add(criteriaBuilder.like(root.get("itemUse").as(String.class), "%" + (String) searchMap.get("itemUse") + "%"));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}
